package com.xxxx.server.controller;


import com.xxxx.server.pojo.Position;
import com.xxxx.server.service.IPositionService;
import com.xxxx.server.vo.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 学海无涯
 * @since 2021-11-15
 */
@RestController
@RequestMapping("/system/basic/pos")
@Api(tags = "职位管理")
public class PositionController {

    @Autowired
    private IPositionService positionService;

    @ApiOperation(value = "获取所有职位信息")
    @GetMapping("/")
    public RespBean<List<Position>> getAllPosition(){
        List<Position> list = positionService.list();
        return new RespBean<List<Position>>().success("ok",list);
    }

    @ApiOperation(value = "添加职位")
    @PostMapping("/")
    public RespBean addPosition(@RequestBody Position position){
        position.setCreateDate(LocalDateTime.now());
        boolean save = positionService.save(position);
        if (save){
            return new RespBean().success("ok");
        }else {
            return new RespBean().error("error");
        }
    }

    @ApiOperation(value = "更新职位信息")
    @PutMapping("/")
    public RespBean editPosition(@RequestBody Position position){
        if (positionService.updateById(position)){
            return new RespBean().success("ok");
        }else {
            return new RespBean().success("error");
        }
    }

    @ApiOperation(value = "删除职位信息")
    @DeleteMapping("/{id}")
    public RespBean delPosition(@PathVariable Integer id){
        if (positionService.removeById(id)){
            return new RespBean().success("ok");
        }else {
            return new RespBean().success("error");
        }
    }

    @ApiOperation(value = "批量删除职位信息")
    @DeleteMapping("/")
    public RespBean delPositionByIds(Integer[] ids){
        if (positionService.removeByIds(Arrays.asList(ids))){
            return new RespBean().success("ok");
        }else {
            return new RespBean().success("error");
        }
    }
}
